package org.s166.ums.util;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 乱码处理
 */
@WebFilter(urlPatterns = "/*",initParams = {@WebInitParam(name="encoding",value="UTF-8")})
public class EncodingFilter implements Filter {
    private String encoding ;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String encoding = filterConfig.getInitParameter("encoding") ;
        this.encoding = encoding ;
        System.out.println("统一编码的初始化: "+encoding);

    }

    @Override
    public void doFilter(ServletRequest sRequest, ServletResponse sResponse, FilterChain chain)
            throws IOException, ServletException {
        //1.向下转型
        HttpServletRequest request = (HttpServletRequest)sRequest ;
        HttpServletResponse response = (HttpServletResponse)sResponse ;
        //2.设置统一编码
        request.setCharacterEncoding(this.encoding);
        //3.放行
        chain.doFilter(request, response);

    }

    @Override
    public void destroy() {
        System.out.println("统一编码过滤器－销毁");
    }
}
